/**
 * Created by wenchuan on 2018/5/10.
 */
define(function () {

    var self,
        app = win.app,
        orders = [],
        defaultInfo = {
            GWAKL003_KH: '',
            GWAKL003_JXS: ''

        },
        address = {
            GWCS011: '',//经销商编号
            GWCS012: '',//经销商总经理
            GWCS013: '',//经销商总经理电话
            GWAKL004_JXS: '',//经销商名称
            GWCS009: '', //客户手机号
            GWCS010: '',//客户编号
            GWCS020: '',//详细地址
            GWAKL004: '',//客户名称
            GWCS001: '',//产品编号
            GWCS107: '',//线人姓名
            GWCS108: '',//线人手机号
            GWCS021: '',//
            GWCS022: '',//
            GWCS023: '',//
            GWCS024: '',//
            GWCS025: '',//
            GWCS026: '',//
            GWCS027: '',
            GWCS028: '',
            GWCSLEGAL: '',//法人
            GWCSLEGAL_NAME: '',//法人编号

        },
        others = {
            countAmount: 0
        },
        pageUi,
        orderSubmitArgs
    ;

    function setCountAmount() {
        var c = 0;
        orders.forEach(function (v) {
            c += (Number(v.GWCQ004) * Number(v.GWCQ003));
        });
        others.countAmount = c;
    }

    var GwcSubmit = app.Module.extend({

        init: function () {
            self = this;
            self._data = {};
            orderSubmitArgs = app.pageParameters['gwcsubmit_page'];
            orders.pushArray(orderSubmitArgs.items);
            defaultInfo = orderSubmitArgs.defaultinfo;
            if (defaultInfo) {
                address.GWAKL004 = defaultInfo.GWAKL003_KH;
                address.GWAKL004_JXS = defaultInfo.GWAKL003_JXS;
                self.getinfo();
            }
            self._super({}, win);
        },
        getinfo: function () {
            if (!defaultInfo.GWCQ010 || !defaultInfo.GWCQ011) {
                return false;
            }
            app.ajax({
                xurl: "api/wx/gwat300/getinfo",
                loading: true,
                xdata: {
                    GWCQ010: defaultInfo.GWCQ010,
                    GWCQ011: defaultInfo.GWCQ011,
                    GWCQLEGAL: defaultInfo.GWCQLEGAL
                },
                cb: function (result) {
                    if (result.ok) {
                        var obj = result.data.data[0];
                        address.GWCS010 = obj.GWAK001;
                        address.GWAKL004 = obj.GWAKL004;
                        address.GWCS107 = obj.GWAK017;//线人姓名
                        address.GWCS108 = obj.GWAK018;//线人手机号
                        address.GWCS021 = obj.GWAK020;
                        address.GWCS022 = obj.GWAK021;
                        address.GWCS023 = obj.GWAK022;
                        address.GWCS024 = obj.GWAK023;
                        address.GWCS025 = obj.GWAK024;
                        address.GWCS026 = obj.GWAK025;
                        address.GWCS027 = obj.GWAK026;
                        address.GWCS028 = obj.GWAK027;

                        address.GWCS011 = obj.GWAK001_JXS;//经销商编号
                        address.GWCS012 = obj.GWAK050_JXS;//总经理
                        address.GWCS013 = obj.GWAK053_JXS;//总经理电话
                        address.GWAKL004_JXS = obj.GWAKL004_JXS;

                        address.GWCSLEGAL = obj.ID_;//法人
                        address.GWCSLEGAL_NAME = obj.NAME_;//法人编号

                    }
                }
            })
        },
        subData: function () {

            address.orders = orders;
            app.ajax({
                xurl: "api/wx/gwat300/updateData",
                loading: true,
                errmsg: '提交操作异常',
                xdata: {
                    obj: app.jsonString(address)
                },
                confirm: {
                    msg: '确定提交订单？'
                },
                before: function (b) {
                    if (!address.GWCS010 || address.GWCS010 == '') {
                        app.toast('请选择客户！');
                        b = false;
                    } else if (!address.GWCS011 || address.GWCS011 == '') {
                        app.toast('请选择经销商！');
                        b = false;
                    }
                    return b;
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '订单提交成功！',
                            cb: function () {
                                app.back(1);
                                orderSubmitArgs.cb();
                            }
                        });
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            setCountAmount();
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        },
        _ready: function () {
            Vue.component('gwcsubmit-template', {
                template: app.getByid(doc, 'cartitem-template'),
                props: {
                    model: Array
                },
                methods: {}
            });

            new Vue({
                el: app.getByid(doc, 'address-box'),
                data: {
                    address: address
                },
                methods: {
                    showCustomers: function () {
                        app.openPage({
                            // src: "gwcxd/customers.html",
                            src: "hn-bfjltb/customers.html",
                            // argsKey: 'customers_page',
                            args: {
                                cb: function (obj) {
                                    address.GWCS010 = obj.GWAK001;
                                    address.GWAKL004 = obj.GWAKL004;
                                    address.GWCS107 = obj.GWAK017;//线人姓名
                                    address.GWCS108 = obj.GWAK018;//线人手机号
                                    address.GWCS021 = obj.GWAK020;
                                    address.GWCS022 = obj.GWAK021;
                                    address.GWCS023 = obj.GWAK022;
                                    address.GWCS024 = obj.GWAK023;
                                    address.GWCS025 = obj.GWAK024;
                                    address.GWCS026 = obj.GWAK025;
                                    address.GWCS027 = obj.GWAK026;
                                    address.GWCS028 = obj.GWAK027;

                                    address.GWCSLEGAL = obj.gwakplant;//法人
                                    address.GWCSLEGAL_NAME = obj.name_;//法人编号
                                }
                            }
                        });
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'jxsaddress-box'),
                data: {
                    address: address
                },
                methods: {
                    showJxs: function () { //经销商+客户

                        app.openPage({
                            src: 'hn-bfjltb1/customers.html',
                            //src: "gwcxd/gwc-jxs-info.html",
                            //argsKey: 'jxsinfo_page',
                            args: {
                                cb: function (obj) {
                                    address.GWCS011 = obj.GWAK001;//经销商编号
                                    address.GWCS012 = obj.GWAK050;//总经理
                                    address.GWCS013 = obj.GWAK053;//总经理电话
                                    address.GWAKL004_JXS = obj.GWAKL004;
                                }
                            }
                        });
                    },
                }
            });

            new Vue({
                el: app.getByid(doc, 'fraddress-box'),
                data: {
                    address: address
                },
                methods: {
                    showFr: function () { //经销商归属法人

                        app.openPage({
                            src: "gwcxd/gwc-fr-info.html",
                            args: {
                                cb: function (obj) {
                                    address.GWCSLEGAL = obj.ID;//法人
                                    address.GWCSLEGAL_NAME = obj.NAME;//法人编号
                                }
                            }
                        });
                    },
                }
            });

            new Vue({
                el: app.getByid(doc, 'contentCenter'),
                data: {
                    items: orders
                },
                methods: {
                    serchData: function () {
                        self._refresh();
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'contentFooter'),
                data: others,
                methods: {
                    subOrder: function () {
                        self.subData();
                    }
                }
            });

        }
    });

    return new GwcSubmit();

});